import { configureStore } from '@reduxjs/toolkit';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import authReducer from './authSlice';

// 持久化配置
const persistConfig = {
    key: 'auth', //存储的键名
    storage,
    // 可选：指定需要持久化的字段（如果只需要部分字段）
    // whitelist: ['role', 'username', 'password']
}

// 创建持久化 reducer
const persistedReducer = persistReducer(persistConfig, authReducer);

// 配置 store
export const store = configureStore({
    reducer: {
        auth: persistedReducer
    },
    // 必须添加 middleware 配置，否则会报错
    middleware: (getDefaultMiddleware) =>
        getDefaultMiddleware({
            serializableCheck: false, //关闭序列化检查，允许存储非序列化数据
        }),
});
// 创建持久化 store
export const persistor = persistStore(store);

// 类型定义
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;